package org.budo.netty.servlet.server;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;

import org.budo.netty.servlet.server.connector.HttpConnector;
import org.budo.netty.servlet.server.event.BudoEventBus;
import org.budo.netty.servlet.server.event.EventBus;
import org.budo.netty.servlet.server.exception.handler.ExceptionHandler;
import org.budo.netty.servlet.server.handler.servlet.BudoServletContext;
import org.budo.netty.servlet.server.handler.servlet.DefaultBudoServletContext;
import org.budo.netty.servlet.server.resource.loader.ResourceLoader;
import org.budo.netty.servlet.server.session.repository.SessionRepository;
import org.budo.netty.servlet.server.session.repository.SessionRepositoryWrapper;

import lombok.Getter;
import lombok.Setter;

/**
 * @author li
 */
@Getter
@Setter
public abstract class AbstractBudoServletServer implements BudoServletServer {
    private List<String> welcomeFiles = new ArrayList<String>();

    private Map<Integer, String> errorPages = new HashMap<Integer, String>();

    private ServletContext servletContext;

    private ResourceLoader resourceLoader;

    private SessionRepository sessionRepository;

    private ExceptionHandler exceptionHandler;

    private HttpConnector httpConnector;

    private EventBus eventBus = new BudoEventBus();

    // lazy init 有必要?
    public ServletContext getServletContext() {
        if (null != this.servletContext) {
            return this.servletContext;
        }

        synchronized (this) {
            BudoServletContext budoServletContext = new DefaultBudoServletContext(this);
            return this.servletContext = budoServletContext;
        }
    }

    public void setSessionRepository(SessionRepository sessionRepository) {
        this.sessionRepository = new SessionRepositoryWrapper(sessionRepository); // wrapper 包一下
    }
}